package com.scopionstudio.newpaper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.analytics.tracking.android.EasyTracker;
import com.scopionstudio.bean.Variables;

public class WebviewLink extends ScopionActivity {
	private WebView webView;
	private ProgressDialog proGress;
	private String link;
	private int paper = 1;
	private int position;
	// private ImageButton imbBack;
//	int position;
//	int paper;
	private WebViewAsyncTask mAsyncTask;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webviewlink);
		initUtils();
		mAsyncTask = new WebViewAsyncTask();
		mAsyncTask.execute(link);
		Bundle bundle = getIntent().getExtras();
		position = bundle.getInt(Variables.CATEGORY);
		paper = bundle.getInt(Variables.PAPER);
	}

	public void initUtils() {
		webView = (WebView) findViewById(R.id.webView);
		Bundle bundle = getIntent().getExtras();
		link = bundle.getString(Variables.LINK);
//		position = bundle.getInt(Variables.CATEGORY);
//		paper = bundle.getInt(Variables.PAPER);
		
		/*
		 * Config Webview.
		 */
		
		
		webView.getSettings().setSupportZoom(true);
		webView.setInitialScale(1);
		webView.getSettings().setLoadWithOverviewMode(true);
		webView.getSettings().setUseWideViewPort(true);
		webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
		webView.setScrollbarFadingEnabled(false);
		webView.setWebViewClient(new WebClient());
	}

	// Asnyctask
	class WebViewAsyncTask extends AsyncTask<String, Void, Void> {
		@Override
		protected void onPreExecute() {
			proGress = new ProgressDialog(WebviewLink.this);
			proGress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			proGress.setMessage(getResources().getString(R.string.ms_load_data));
			proGress.setCancelable(true);
			proGress.setCanceledOnTouchOutside(false);
			proGress.setOnCancelListener(new OnCancelListener() {
				
				@Override
				public void onCancel(DialogInterface dialog) {
					dialog.cancel();
					dialog.dismiss();
					mAsyncTask.cancel(true);
				}
			});
			proGress.show();
		}
		@Override
		protected Void doInBackground(String... params) {
			webView.loadUrl(params[0]);
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
//			proGress.dismiss();
		}
	}

	// Custome webclient
	class WebClient extends WebViewClient {
		@Override
		public void onPageFinished(WebView view, String url) {
			if (proGress != null) {
				proGress.dismiss();
			}
			super.onPageFinished(view, url);
		}
	}
	@Override
	public void onBackPressed() {
		Intent mIntent = new Intent(WebviewLink.this,ShowInfo.class);
		mIntent.putExtra(Variables.PAPER, paper);
		mIntent.putExtra(Variables.CATEGORY, position);
		startActivity(mIntent);
		finish();
	}
    @Override
    protected void onStop() {
        super.onStop();
        EasyTracker.getInstance(getApplicationContext()).activityStop(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        EasyTracker.getInstance(getApplicationContext()).activityStart(this);
    }


}
